package com.yiwenup.leetcode.top_interview_150;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 文件描述
 *
 * @Project struct-algorithm
 * @Package com.yiwenup.leetcode.top_interview_150
 * @Author yiwenup
 * @Date 2024-12-21 14:08:23
 * @Description https://leetcode.cn/problems/group-anagrams/?envType=study-plan-v2&envId=top-interview-150
 */
public class No0049 {
    public List<List<String>> groupAnagrams(String[] strs) {
        Map<String, List<String>> map = new HashMap<>();
        for (String str : strs) {
            char[] cs = str.toCharArray();
            Arrays.sort(cs);
            String key = new String(cs);
            List<String> group = map.get(key);
            if (group == null) {
                group = new ArrayList<>();
                group.add(str);
                map.put(key, group);
            } else {
                group.add(str);
            }
        }
        return new ArrayList<List<String>>(map.values());
    }
}
